package cn.yuanqiao.archive.appManager.service.impl;

import cn.yuanqiao.archive.appManager.domain.YqWhzrz;
import cn.yuanqiao.archive.appManager.mapper.YqWhzrzMapper;
import cn.yuanqiao.archive.appManager.service.IYqWhzrzService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 文号责任者Service业务层处理
 * 
 * @author sy
 * @date 2023-09-12
 */
@Service
public class YqWhzrzServiceImpl implements IYqWhzrzService
{
    @Autowired
    private YqWhzrzMapper yqWhzrzMapper;

    /**
     * 查询文号责任者
     * 
     * @param ID 文号责任者主键
     * @return 文号责任者
     */
    @Override
    public YqWhzrz selectYqWhzrzByID(Long ID)
    {
        YqWhzrz yqWhzrz = new YqWhzrz();
        yqWhzrz.setId(ID);
        return yqWhzrzMapper.selectYqWhzrzByID(yqWhzrz);
    }

    /**
     * 查询文号责任者列表
     * 
     * @param yqWhzrz 文号责任者
     * @return 文号责任者
     */
    @Override
    public List<YqWhzrz> selectYqWhzrzList(YqWhzrz yqWhzrz)
    {
        return yqWhzrzMapper.selectYqWhzrzList(yqWhzrz);
    }

    /**
     * 新增文号责任者
     * 
     * @param yqWhzrz 文号责任者
     * @return 结果
     */
    @Override
    public String insertYqWhzrz(YqWhzrz yqWhzrz)
    {
        StringBuilder successMsg = new StringBuilder();
        if (yqWhzrzMapper.isOnly(yqWhzrz)!=0){
            successMsg.append("全宗“"+yqWhzrz.getFondsName()+"”下已存在值为"+yqWhzrz.getWhzrz()+"的文号责任者！");
        }else {
            LoginUser user = SecurityUtils.getLoginUser();

//            if(user == null){
//                user = new LoginUser();
//                user.setUserId(1L);
//            }
            yqWhzrz.setCreatorName(user.getUsername());
            yqWhzrz.setCreatorId(user.getUserId());
            yqWhzrz.setCreateTime(DateUtils.getNowDate());
            if(yqWhzrzMapper.insertYqWhzrz(yqWhzrz)>0){
                successMsg.append("添加成功！");
            };
        }
        return successMsg.toString();
    }

    /**
     * 修改文号责任者
     * 
     * @param yqWhzrz 文号责任者
     * @return 结果
     */
    @Override
    public String updateYqWhzrz(YqWhzrz yqWhzrz)
    {
        StringBuilder successMsg = new StringBuilder();
        if (yqWhzrzMapper.isOnly(yqWhzrz)!=0){
            successMsg.append("全宗“"+yqWhzrz.getFondsName()+"”下已存在值为"+yqWhzrz.getWhzrz()+"的文号责任者！");
        }else {
            LoginUser user = SecurityUtils.getLoginUser();

//            if(user == null){
//                user = new LoginUser();
//                user.setUserId(1L);
//            }

            yqWhzrz.setLastupdater(user.getUserId());
            yqWhzrz.setLastupdateTime(DateUtils.getNowDate());
            if(yqWhzrzMapper.updateYqWhzrz(yqWhzrz)>0){
                successMsg.append("修改成功！");
            };
        }
        return successMsg.toString();
    }

    /**
     * 批量删除文号责任者
     * 
     * @param IDs 需要删除的文号责任者主键
     * @return 结果
     */
    @Override
    public int deleteYqWhzrzByIDs(Long[] IDs)
    {
        YqWhzrz yqWhzrz = new YqWhzrz();
        return yqWhzrzMapper.deleteYqWhzrzByIDs(yqWhzrz.getTableName(),IDs);
    }

    /**
     * 删除文号责任者信息
     * 
     * @param ID 文号责任者主键
     * @return 结果
     */
    @Override
    public int deleteYqWhzrzByID(String ID)
    {
        return yqWhzrzMapper.deleteYqWhzrzByID(ID);
    }
}
